package com.lzb.yabbs.app;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lzb.yabbs.AppConfig;
import com.lzb.yabbs.R;
import com.lzb.yabbs.dao.UserInfo;
import com.lzb.yabbs.interfaces.LoginCallback;
import com.lzb.yabbs.net.LoginToNet;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mLoginBtn;
    private EditText mUsernameEt;
    private EditText mPsdEt;
    private UserInfo mUserInfo;
    private TextInputLayout mUsernameTil;
    private TextInputLayout mPsdTil;

    private final static int LOGIN_SUCESS=0x142;
    private final static int LOGIN_FAIL=0x125;

    public Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case LOGIN_SUCESS:
                    Intent intent=new Intent();
                    intent.putExtra("userinfo",mUserInfo);
                    setResult(LOGIN_SUCESS,intent);
                    finish();
                    break;
                case LOGIN_FAIL:
                    int reason=msg.getData().getInt("reason",0);
                    if(reason==LoginToNet.LOGIN_PSD_USERNAME_ERRO){
                        Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(),"未知错误",Toast.LENGTH_SHORT).show();
                    }
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        restoreActionbar();
    }

    private void initView(){
        mLoginBtn= (Button) findViewById(R.id.login_btn);
        mUsernameTil= (TextInputLayout) findViewById(R.id.username_til);
        mPsdTil= (TextInputLayout) findViewById(R.id.psd_til);
        mUsernameEt=mUsernameTil.getEditText();
        mPsdEt=mPsdTil.getEditText();
        mLoginBtn.setOnClickListener(this);
        mUsernameTil.setHint("账号");
        mPsdTil.setHint("密码");

    }

    @Override
    public void onClick(View v) {
        if(R.id.login_btn==v.getId()){
            LoginToNet.login(AppConfig.URL_LOGIN, new LoginCallback() {
                @Override
                public void loginSuccess(UserInfo userInfo) {
                    mUserInfo=userInfo;
                    handler.sendEmptyMessage(LOGIN_SUCESS);
                }

                @Override
                public void loginFail(int reason) {
                    Bundle bundle=new Bundle();
                    bundle.putInt("reason", reason);
                    Message msg=new Message();
                    msg.what=LOGIN_FAIL;
                    msg.setData(bundle);
                    handler.sendMessage(msg);
                }
            },mUsernameEt.getText().toString(),mPsdEt.getText().toString());
        }
    }

    private void restoreActionbar(){
        ActionBar actionBar=getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("登录");
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}
